<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      @submit.native.prevent
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px">
      <el-form-item label="jan编码" prop="janCode">
        <el-input v-model="queryParams.janCode" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="importInfo.show = true"
          v-hasPermi="['distribution:hsCodeRate:importData']">
          <!-- 导入 -->
          {{ $t('导入') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleExport"
          v-hasPermi="['distribution:janCode:exportJanCode']">
          <!-- 导出 -->
          {{ $t('导出') }}
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      :height="calTableHeight()"
      v-loading="loading"
      ref="TableRef"
      :data="hsCodeRateList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="jan编码" align="center" prop="janCode" />
      <el-table-column label="是否被使用" align="center" prop="marker">
        <template slot-scope="{ row }">
          <!-- 是否被占用 0-否 1-是 -->
          <div v-if="row.marker == 0" style="color: red">未使用</div>
          <div v-if="row.marker == 1" style="color: green">已使用</div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:janCode:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <SkuDetail :show.sync="skuDetail.show" @getList="getList" :formData="skuDetail.formData" />

    <Import
      action="/distribution/janCode/importJanCode"
      templateUrl="distribution/janCode/exportTemp"
      @success="getList"
      :show.sync="importInfo.show">
      <!-- :query="{ isCover: importInfo.query.isCover ? 1 : 0 }" -->
      <!-- text-align: center -->
      <!-- <div style="margin: 10px 0">
        <el-checkbox v-model="importInfo.query.isCover">是否覆盖相同JAN编码</el-checkbox>
      </div> -->
    </Import>
  </div>
</template>

<script>
import Import from '@/components/Import/index.vue'

import { janCodeList, deleteJanCode } from '@/api/basicSetting/janCode'

import SkuDetail from './components/skuDetail.vue'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'JanCode',
  components: { SkuDetail, Import },
  data() {
    return {
      /**
       * 导入组件的显示与隐藏
       */
      importInfo: {
        show: false,
        query: {
          // isCover 是否覆盖 0-否 1-是
          isCover: false,
        },
      },
      /**sku详情 */
      skuDetail: {
        show: false,
        formData: {},
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 关税税率表格数据
      hsCodeRateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hsCode: undefined,
        codeRate: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hsCode: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeRate: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleExport() {
      this.download(
        'distribution/janCode/exportJanCode',
        {
          ...this.queryParams,
        },
        `janCode_${new Date().getTime()}.xlsx`,
      )
    },
    /** 查询关税税率列表 */
    getList() {
      this.loading = true
      janCodeList(this.queryParams).then((response) => {
        this.hsCodeRateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        hsCode: undefined,
        codeRate: undefined,
        createBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        updateBy: undefined,
        remark: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      getHsCodeRate(id).then((response) => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改关税税率'
      })
    },
    /** 查看sku详情 */
    handleDetail(row) {
      this.skuDetail.show = true
      this.skuDetail.formData = row
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateHsCodeRate(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addHsCodeRate(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        deleteJanCode(row.id).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
  },
}
</script>
